using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using FireStarterModeller.BO;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using AnchorStyles = Habanero.Faces.Base.AnchorStyles;
using DockStyle = Habanero.Faces.Base.DockStyle;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// This control contains the a <see cref="CollapsiblePanelGroupControlWin"/> which is configured to 
    /// Contain panels for editing the <see cref="DMClass"/>s attributes, Properties, relationships and Unique Constraints.
    /// </summary>
    public class DMClassDetailEditorControl : PanelWin, IBusinessObjectControl
    {
        private readonly IFireStarterFormsController _formsController;
        private ICollapsiblePanelGroupControl _collapsiblePanelGroupControl;
        private DMClass _dmClass;
        private DMPropertyAndRuleEditorControl _dmPropertyAndRuleEditorControl;
        private DMRelationshipsControl _dmRelationshipsControl;
        private DMUniqueConstraintAndPropertyEditorControl _uniqueConstraintPropsControl;
        private DMClassEditorControl _dmClassEditorControl;
        private DMSolution _solution;
        private readonly ITabControl _tabControl;
        private readonly ITabPage _uiTabPage;
        public IUIViewsEditor UIViewsEditor { get; set; }

        //        public DMClassDetailEditorControl()
        //        {
        //
        //        }

        public DMClassEditorControl DMClassEditorControl
        {
            get { return _dmClassEditorControl; }
        }

        public DMClassDetailEditorControl(IControlFactory controlFactory, IFireStarterFormsController formsController)
        {
            _formsController = formsController;
            ControlFactory = controlFactory;

            _tabControl = controlFactory.CreateTabControl();
            ITabPage dmTabPage = controlFactory.CreateTabPage("Domain Model");
            _tabControl.TabPages.Add(dmTabPage);
            _uiTabPage = controlFactory.CreateTabPage("UI Model");
            _tabControl.TabPages.Add(_uiTabPage);

            SetupDMTabPage(dmTabPage);

            SetupUITabPage(_uiTabPage);

            _tabControl.SelectedIndexChanged += (sender, args) =>
            {
                if (_tabControl.SelectedTab == _uiTabPage) SetupDMClassUIModel();
            };

            BorderLayoutManager layoutManager = ControlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_tabControl, BorderLayoutManager.Position.Centre);
        }

        private void SetupUITabPage(ITabPage uiTabPage)
        {
            this.UIViewsEditor = new XmlUIViewsEditorControl(this.ControlFactory, _formsController);
            BorderLayoutManager dmTabLayoutManager = ControlFactory.CreateBorderLayoutManager(uiTabPage);
            dmTabLayoutManager.AddControl(this.UIViewsEditor, BorderLayoutManager.Position.Centre);
        }

        private void SetupDMTabPage(ITabPage dmTabPage)
        {
            CreateCollapsiblePanels();
            PanelWin scrollablePanel = (PanelWin)ControlFactory.CreatePanel();
            scrollablePanel.AutoScroll = true;
            //new ColumnLayoutManager(scrollablePanel, ControlFactory);
            _collapsiblePanelGroupControl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            _collapsiblePanelGroupControl.Width = scrollablePanel.Width - scrollablePanel.AutoScrollMargin.Width;
            scrollablePanel.Controls.Add((Control)_collapsiblePanelGroupControl);
            TrackCollapsiblePanelHeight();

            //scrollablePanel.Dock = (DockStyle) Habanero.Faces.Base.DockStyle.Fill;


            BorderLayoutManager dmTabLayoutManager = ControlFactory.CreateBorderLayoutManager(dmTabPage);
            dmTabLayoutManager.AddControl(scrollablePanel, BorderLayoutManager.Position.Centre);
        }

        private void TrackCollapsiblePanelHeight()
        {
            List<ICollapsiblePanel> panelsList = _collapsiblePanelGroupControl.PanelsList;
            foreach (ICollapsiblePanel collapsiblePanel in panelsList)
            {
                collapsiblePanel.Uncollapsed += (sender, e) => UpdateCollapsiblePanelHeight();
            }
            UpdateCollapsiblePanelHeight();
        }

        private void UpdateCollapsiblePanelHeight()
        {
            List<ICollapsiblePanel> panelsList = _collapsiblePanelGroupControl.PanelsList;
            int gapSize = _collapsiblePanelGroupControl.ColumnLayoutManager.GapSize;
            int totalHeight = 0;
            foreach (ICollapsiblePanel collapsiblePanel in panelsList)
            {
                totalHeight += collapsiblePanel.Height + gapSize;
            }
            _collapsiblePanelGroupControl.Height = totalHeight;
        }

        private void CreateCollapsiblePanels()
        {
            _collapsiblePanelGroupControl = new CollapsiblePanelGroupControlWin();
            _dmClassEditorControl = new DMClassEditorControl(this.ControlFactory);
                // {UIViewsEditor = new PopupXmlUIViewsEditor(this.ControlFactory)}
            
            ICollapsiblePanel collapsiblePanel = _collapsiblePanelGroupControl.AddControl(_dmClassEditorControl, "Entities/Classes", _dmClassEditorControl.Height);
            collapsiblePanel.Pinned = true;
            _dmPropertyAndRuleEditorControl = new DMPropertyAndRuleEditorControl(this.ControlFactory);
            collapsiblePanel = _collapsiblePanelGroupControl.AddControl(_dmPropertyAndRuleEditorControl, "Properties", _dmPropertyAndRuleEditorControl.Height);
            collapsiblePanel.Pinned = true;
            _dmRelationshipsControl = new DMRelationshipsControl(this.ControlFactory);
            _collapsiblePanelGroupControl.AddControl(_dmRelationshipsControl, "Relationships", _dmRelationshipsControl.Height);
            _uniqueConstraintPropsControl = new DMUniqueConstraintAndPropertyEditorControl(this.ControlFactory);
            _collapsiblePanelGroupControl.AddControl(_uniqueConstraintPropsControl, "Unique Constraints", _uniqueConstraintPropsControl.Height);
            _collapsiblePanelGroupControl.Height = _collapsiblePanelGroupControl.TotalExpandedHeight;
        }

        public IControlFactory ControlFactory { get; private set; }

        public List<ICollapsiblePanel> PanelList
        {
            get { return _collapsiblePanelGroupControl.PanelsList; }
        }

        public DMClass DMClass
        {
            get { return _dmClass; }
            set
            {
                try
                {
                    if (_dmClass == value) return;
                    _dmClass = value;
                    
                   // this.UIViewsEditor.EditUIViews(_dmClass);
                    this.DMClassEditorControl.BusinessObject = _dmClass;
                    this.DMPropertyAndRuleEditorControl.Class = _dmClass;
                    this.DMRelationshipsControl.Class = _dmClass;
                    this.UniqueConstraintPropertyEditorControl.Class = _dmClass;
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
        }

        private void SetupDMClassUIModel()
        {
            try
            {
                this.UIViewsEditor.EditUIViews(_dmClass);
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
            }
        }

        public DMPropertyAndRuleEditorControl DMPropertyAndRuleEditorControl
        {
            get { return _dmPropertyAndRuleEditorControl; }
        }

        public DMRelationshipsControl DMRelationshipsControl
        {
            get { return _dmRelationshipsControl; }
        }

        public DMUniqueConstraintAndPropertyEditorControl UniqueConstraintPropertyEditorControl
        {
            get { return _uniqueConstraintPropsControl; }
        }

        public IBusinessObject BusinessObject
        {
            get { return this.DMClass; }
            set { this.DMClass = (DMClass)value; 
             if (_tabControl.SelectedTab == _uiTabPage)
             {
                    UIViewsEditor.EditUIViews(this.DMClass);
             }
            }
        }

        /// <summary>
        /// gets and sets the <see cref="DMSolution"/> used in this control
        /// </summary>
        public DMSolution Solution
        {
            get { return _solution; }
            set
            {
                _solution = value;
                this.DMClass = null;
                DMRelationshipSelectorControl relationshipSelectorControl = (DMRelationshipSelectorControl)this.DMRelationshipsControl.RelationshipSelector;
                relationshipSelectorControl.Solution = _solution;
            }
        }
    }
}
